package pers.tz.题目;

/**
 * 求1! + 2! + 3! + ... + N!
 *
 * @author irony
 * @date 2024-03-09
 */
public class 算阶乘 {

    public static void main(String[] args) {
        getJiecheng01(60000);
        getJiecheng02(60000);
    }

    // 可行解
    public static void getJiecheng01(int N) {
        long start = System.currentTimeMillis();
        int sum = 0;
        for (int i = 1; i <= N; i++) {
            int sum2 = 1;
            for (int j = 1; j <= i; j++) {
                sum2 *= j;
            }
            sum += sum2;
        }
        System.out.println("time = " + (System.currentTimeMillis() - start) + ", sum = " + sum);
    }

    // 最优解
    public static void getJiecheng02(int N) {
        long start = System.currentTimeMillis();
        int sum = 0;
        int tempSum = 1;
        for (int i = 1; i <= N; i++) {
            tempSum = tempSum * i;
            sum += tempSum;
        }
        System.out.println("time = " + (System.currentTimeMillis() - start) + ", sum = " + sum);
    }

}
